﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.Service.Entity;

namespace ZSZ.Service
{
    public class RegionService : IRegionService
    {
        private RegionDTO ToDTO(RegionEntity e)
        {
            RegionDTO dto = new RegionDTO();
            dto.CityID = e.CityId;
            dto.CreateTime = e.CreateDatatime;
            dto.Id = e.Id;
            dto.Name = e.Name;
            return dto;
        }
        public RegionDTO[] GetAll(long cityId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RegionEntity> cs = new CommonService<RegionEntity>(ct);
                return cs.GetAll().Where(r => r.CityId == cityId).ToList().Select(r => ToDTO(r)).ToArray();
            }
        }

        public RegionDTO GetById(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RegionEntity> cs = new CommonService<RegionEntity>(ct);
                var region = cs.GetById(id);
                if (region==null)
                {
                    return null;
                }
                return ToDTO(region);
            }
        }

        public RegionDTO[] GetByCityId(long cityId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RegionEntity> cs = new CommonService<RegionEntity>(ct);
               return cs.GetAll().Where(r => r.CityId == cityId).ToList().Select(r=>ToDTO(r)).ToArray();
            }
        }
    }
}
